package com.jie.jie.camera;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.ImageFormat;
import android.hardware.Camera;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jie.jie.R;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class DIYCameraActivity extends AppCompatActivity implements SurfaceHolder.Callback {

    @Bind(R.id.diy_back_tv)
    TextView diyBackTv;
    @Bind(R.id.diy_camera_sv)
    SurfaceView diyCameraSv;
    @Bind(R.id.diy_camera_btn)
    Button diyCameraBtn;
    @Bind(R.id.activity_diycamera)
    LinearLayout activityDiycamera;
    @Bind(R.id.diy_show_iv)
    ImageView diyShowIv;
    private Camera camera;
    private SurfaceHolder holder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_diycamera);
        ButterKnife.bind(this);
        holder = diyCameraSv.getHolder();
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        holder.addCallback(this);
        camera = getCamera(0);
        Camera.Parameters params = camera.getParameters();
        params.setPictureFormat(ImageFormat.JPEG);
        params.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
        camera.setParameters(params);
//自动聚焦
        camera.autoFocus(new Camera.AutoFocusCallback() {
            public void onAutoFocus(boolean success, Camera camera) {
                if (success)
                    System.out.println("聚焦成功 !");
                else
                    System.out.println("聚焦失败 !");
            }
        });


    }

    //第三个PictureCallback接口回调,通过data[]保持图片数据信息
    private Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {


        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            Bitmap bitmap = null;
            if (data.length != 0) {
                bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
            }
            if (bitmap != null) {
                diyCameraSv.setVisibility(View.INVISIBLE);
                diyShowIv.setImageBitmap(bitmap);
                diyShowIv.setVisibility(View.VISIBLE);
            }
//            File pictureFile = MainActivity.getOutputMediaFile(MEDIA_TYPE_IMAGE);
//            if (pictureFile == null){
//                return;
//            }
//            try {
//                FileOutputStream fos = new FileOutputStream(pictureFile);
//                fos.write(data);
//                fos.close();
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//            Toast.makeText(RecordVedioAct.this, "图像已保存", Toast.LENGTH_SHORT).show();
//            camera.startPreview(); //拍完继续预览
        }
    };

    public final void takePicture(Camera.ShutterCallback shutter, Camera.PictureCallback raw, Camera.PictureCallback jpeg) {

    }

    @OnClick({R.id.diy_back_tv, R.id.diy_camera_btn})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.diy_back_tv:
                finish();
                break;
            case R.id.diy_camera_btn:
                //拍照
                if (camera != null) {
                    camera.takePicture(null, null, mPictureCallback);
                }
                break;
        }
    }

    private Camera getCamera(int i) {
        Camera camera;
        try {
            camera = Camera.open(i);
        } catch (Exception e) {
            e.printStackTrace();
            camera = null;
        }
        return camera;
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        try {
            camera = getCamera(0);
            camera.setPreviewDisplay(holder); //camera关联到SurfaceView
            camera.setDisplayOrientation(90); //旋转90度
            camera.startPreview(); //开始预览
        } catch (Exception e) {
            finish();
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        if (camera != null) {
            try {
                camera.setPreviewCallback(null);
                camera.stopPreview();
                camera.release();
                camera = null;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
